class GameMap extends DisplayBase{
	public constructor() {
		super();
	}

	private map_data:Object;
	private wall_data:Array<DisplayBase>;
	private npc_data:Array<DisplayBase>;
	private monster_data:Array<DisplayBase>;
	private map_id:number = 0;
	public p2World:p2.World;
	public stageWidth:number = 0;
    public stageHeight:number = 0;
	public _body_arr:Array<P2Body>;
	public init(map_id:number = 1){
		this.map_id = map_id;
		

		this.stageWidth = GameData.stageWidth;//egret.MainContext.instance.stage.stageWidth;
    	this.stageHeight = GameData.stageHeight;//egret.MainContext.instance.stage.stageHeight;
		//加载文件
		var map_res = this.load_map(map_id);

		this.p2World = new p2.World();
        this.p2World.sleepMode = p2.World.BODY_SLEEPING;
        this.p2World.gravity[1] = -15;

		this.wall_data = new Array();
		var walls = map_res['wall'];
		var wall_length = walls.length;
		console.log('wall_length='+wall_length);
		console.log('this.stageWidth='+this.stageWidth);
		console.log('this.stageHeight='+this.stageHeight);
		var wall = null;
		for(var i=0;i<wall_length;i++){
			wall = new Wall(this);
			wall.init(walls[i]);
			this.wall_data.push(wall);
			console.log(wall.x,wall.y,wall.width,wall.height);
			this.addChild(wall);
		}

		this.createDebug();
		this.process();
		// this.addChild();
		// this.p2World
	}

	private load_map(map_id:number){
		// this.createBody(20,500,stageWidth-20,stageHeight-500,'zfx_png',world);
        // this.createBody(30,500,0,stageHeight-500,'zfx_png',world);
        // this.createBody(stageWidth,30,0,stageHeight-30,'zfx_png',world);
        // this.createBodyAngle(20,200,stageWidth/2,stageHeight-140,'rect',world,45);
        // this.createMan(60,60,100,100,'man_png',world);
		var map_res = {'wall':[],npc:[],monster:[]};
		// console.log('this.stageHeight='+this.stageHeight);
		// map_res['wall'].push({w:32,h:320,x:0,y:this.stageHeight-500,resid:'zfx_png',r:0});
		// map_res['wall'].push({w:1280,h:64,x:0,y:this.stageHeight-64,resid:'title_png',r:0});
		// map_res['wall'].push({w:200,h:32,x:300,y:this.stageHeight-100,resid:'title_png',r:(30*(Math.PI / 180))});


		
		// map_res['wall'].push({w:this.stageWidth,h:32,x:100,y:this.stageHeight-150,resid:'title_png',r:0});
		
		return map_res;
	}

	private process(){
		egret.Ticker.getInstance().register(function(dt) {
            if (dt < 10) {
                return;
            }
            if (dt > 1000) {
                return;
            }
            this.p2World.step(dt / 1000);
			this.debugDraw.drawDebug();
            var stageHeight: number = this.stage.stageHeight;//egret.MainContext.instance.stage.stageHeight;
            var l = this.p2World.bodies.length;
			
            for (var i: number = 0; i < l; i++) {
                var boxBody: p2.Body = this.p2World.bodies[i];
				
				boxBody.position = [boxBody.position[0] + GameData.mapPos[0]*0.02,boxBody.position[1]];
				if(boxBody.displays.length > 0){
					var box: egret.DisplayObject = boxBody.displays[0];
					if (box) {
						box.x = boxBody.position[0] * this.factor;
						box.y = stageHeight - boxBody.position[1] * this.factor;
						box.rotation = 360 - (boxBody.angle + boxBody.shapes[0].angle) * 180 / Math.PI;
					}
				}
			}
			GameData.mapPos[0] = 0;
			GameData.mapPos[1] = 0;
			if(GameData.mapPos[0]!=0){
				console.log(GameData.mapPos[0]);
			}
        }, this);
	}

	/**
	 * 添加刚体
	 */
	public AddBody(dis:eui.Rect){
		if(!this._body_arr)
			this._body_arr = new Array();
		
		var bodys:P2Body = this.createBody(dis.width,dis.height,dis.x,dis.y,this.p2World,dis.rotation,dis);
		this._body_arr.push(bodys);

		// var wall:Wall = new Wall(this);
		// wall.init({w:dis.width,h:dis.height,x:dis.x,y:dis.y,resid:'tile_png',r:0});
		// this.wall_data.push(wall);
		// console.log(wall.x,wall.y,wall.width,wall.height);
		// this.addChild(wall);

	}

	private debugDraw: p2DebugDraw;
	private createDebug(): void {
        //创建调试试图
        this.debugDraw = new p2DebugDraw(this.p2World);
		var sprite: egret.Sprite = new egret.Sprite();
        this.addChild(sprite);
        this.debugDraw.setSprite(sprite);
    }
}